package genia.Interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.interceptor.Interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//拦截器
@Interceptor
public class MyInterceptor  implements HandlerInterceptor {

    //若返回true执行下一个拦截器，若返回false不执行下一个拦截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 若为登陆页面将放行
        System.out.println("uri: " + request.getRequestURI());
        if (!request.getRequestURI().contains("download")) {
            return true;
        }
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("user"));
        // 用户登陆成功将放行
        if (session.getAttribute("user") != null) {
            return true;
        }
        // 用户未登陆跳至登陆页面
        request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
        return false;
    }
}